<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Black_list extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->require_login();
		$this->load->model('black_list_model', 'black_list');
	}

	function index() 
	{
		check_permission("black_list", "view");
		
		$data = array();
		$GET = $this->input->get(NULL, true);
 		$this->config->set_item('enable_query_strings', TRUE);
		$config['base_url'] = base_url().$this->uri->segment(1).'/?';
		$config['per_page'] = '20';
 		$config['num_links'] = 5;	
		$start = $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$results = $this->black_list->getListingData($GET,$start);
		$data['start'] = $start;
		$data['results'] = $results['results'];
		$data['total_record'] = $results['total_record'];
		
		if(is_array(@$GET))	{
			unset($GET['per_page']);
 			$qs = http_build_query($GET); 
		} else {
			$qs = '';	
		}
		
		if(isset($qs) && !empty($qs)){
		   $this->pagination->suffix = "&".$qs;	
		}
		
		$config['total_rows'] = $data['total_record'];
		$config['full_tag_open'] = '<ul class="paging">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$perPage = $config['per_page'];
		$this->config->set_item('enable_query_strings', TRUE);
		$this->pagination->initialize($config);
	
		
		$this->load->view('black_list/listing', $data);
	}

	function save($id = 0) 
	{
		$id = (int)$id;
		
		if( $id > 0 ) {
			check_permission("black_list", "edit");
		} else {
			check_permission("black_list", "add");
		}
		
		$data = array();
		
		$this->form_validation->set_rules('blacklist_cli', 'CLI', 'trim|required|callback_check_duplicate['.$id.']|xss_clean');
		$this->form_validation->set_rules('campaign_type', 'Campaign Type', 'trim|required|xss_clean');
		$this->form_validation->set_rules('active', 'Status', 'trim|required|xss_clean');
		
		if ($this->form_validation->run() == FALSE)
		{
			if ( $id > 0)
			{
				$detail = $this->black_list->getDetailByID( $id );
				foreach($detail as $k=>$v):
				$data[$k] = $v;
				endforeach;
				$data['id'] = $id;
			}
		
			$this->load->view('black_list/save', $data);
		} 
		else 
		{
			$record = array( 
							'blacklist_cli' => addslashes($this->input->post('blacklist_cli')),
							'campaign_type' => addslashes($this->input->post('campaign_type')),
							'active' => addslashes($this->input->post('active')) 
							);	
			
			$this->black_list->save($record, $id);
			$this->session->set_flashdata('userMesg', 'Record has been updated successfully.');
			redirect('black_list/');
		}
	}// end save
  
 	function check_duplicate($blacklist_cli, $id ="")
    {
		$POST = $this->input->post(NULL, true);
		$id = (int)$id;
		
		if( $POST['blacklist_cli'] == '' || $POST['campaign_type'] == '' ) 
		{
			return true;	
		}
		
		$where = " blacklist_cli = '".mysql_real_escape_string($POST['blacklist_cli'])."'";
		$where .= " AND campaign_type = '".mysql_real_escape_string($POST['campaign_type'])."'";
		if( $id > 0 ) 
		{
			$where .= " AND id <> ".$id." ";
		}
 		if(! $this->common_model->record_exist("blacklist", $where))
		{
			return true;	
		}
		else
		{
			$this->form_validation->set_message('check_duplicate', 'The CLI and Campaign Type must be unique.');
			return false;
		}
    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */